﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kovan.Model.Parameter.Config;
using Kovan.BLL.Config;
using Kovan.Model.ViewModel.Config;
using Kovan.Model.Entity.Config;
using System.IO;
using Kovan.Model.Enums;
using Kovan.Library.Configuration;
using Kovan.Library.IO;

namespace Kovan.UI.Areas.Manage.Controllers
{
    public class NewsController : ManageControllerBase
    {
        //
        // GET: /Manage/News/

        public ActionResult Index(PNews param)
        {
            if (!(param.PageSize > 0))
            {
                param.PageSize = 20;
            }
            if (!(param.PageIndex > 0))
            {
                param.PageIndex = 1;
            }
            var items = BNewsManage.GetInstances().GetNewsItems(param);

            ViewBag.param = param;
            return PartialView("index", items);
        }

        public ActionResult GetOperate(long? id)
        {
            ENews item = new ENews();
            if (id.HasValue && id.Value > 0)
            {
                item = BNewsManage.GetInstances().GetNewsItem(id.Value);
            }
            return PartialView("Operate", item);
        }

        [ValidateInput(false)]
        public ActionResult SaveNews(tb_news newsBase, tb_newsData newsData)
        {
            int result = 0;
            string statuscode = string.Empty;
            if (newsBase.news_id.HasValue)
            {
                var eNews = BNewsManage.GetInstances().GetNewsItem(newsBase.news_id.Value);

                eNews.NewsBase.title = newsBase.title;
                eNews.NewsBase.title_color = newsBase.title_color;
                eNews.NewsBase.title_pic = newsBase.title_pic;
                eNews.NewsBase.source = newsBase.source;
                eNews.NewsBase.source_url = newsBase.source_url;
                eNews.NewsBase.is_index = newsBase.is_index;
                eNews.NewsBase.is_view = newsBase.is_view;
                eNews.NewsBase.is_top = newsBase.is_top;
                eNews.NewsBase.modify_date = newsBase.modify_date;
                eNews.NewsBase.auditor = newsBase.auditor;

                eNews.NewsData.news_id = newsBase.news_id;
                eNews.NewsData.news_ukey = newsBase.ukey;
                eNews.NewsData.news_keyword = newsData.news_keyword;
                eNews.NewsData.news_description = newsData.news_description;
                eNews.NewsData.news_content = newsData.news_content;

                result = BNewsManage.GetInstances().Update(eNews);
            }
            else
            {
                newsBase.create_date = DateTime.Now;
                newsBase.create_user = 0;
                newsBase.modify_date = DateTime.Now;
                newsBase.ukey = Guid.NewGuid().ToString("N");
                BNews.GetInstances().Add(newsBase);
                newsData.news_id = newsBase.news_id;
                newsData.news_ukey = newsBase.ukey;
                result = BNewsData.GetInstances().Add(newsData);
            }

            if (result > 0)
            {

                if (!string.IsNullOrEmpty(newsBase.title_pic) && newsBase.title_pic.ToLower().Trim() != "folder.gif")
                {
                    string sourePath = Server.MapPath("~/" + ConfigurationUtility.AppSettings<string>("PictureTemp", "content/Uploads/Temp/") + newsBase.title_pic);
                    var targetPath = Server.MapPath("~/" + ConfigurationUtility.AppSettings<string>("NewsPic", "content/Uploads/News/") + newsBase.title_pic);

                    FileUtility.MoveFile(sourePath, targetPath, true);
                }

                //成功
                statuscode = GetSucceedCode("保存成功", "news_operate", Url.Content("~/Manage/News/GetOperate"), "添加新闻");
            }
            else
            {
                //失败
                statuscode = GetFailCode("保存失败", "", "", "");
            }
            return Content(statuscode);
        }

        public ActionResult Delete(string ids)
        {
            var statuscode = string.Empty;
            List<string> idss = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var result = BNews.GetInstances().BatchDelete(idss);
            if (result > 0)
            {
                statuscode = GetSucceedCode("操作成功", "news_manage", Url.Content("~/Manage/News"), "新闻");
            }
            return Content(statuscode);
        }

        public ActionResult UploadDialog()
        {
            return PartialView("UploadDialog");
        }

        public ContentResult SaveTitlePic()
        {
            var statuscode = string.Empty;
            var file1 = Request.Files["file1"];
            if (file1 != null)
            {
                try
                {
                    var PictureTemp = ConfigurationUtility.AppSettings<string>("PictureTemp", "content/Uploads/Temp/");
                    // 文件上传后的保存路径
                    string filePath = Server.MapPath("~/" + PictureTemp);
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }

                    string fileName = Path.GetFileName(file1.FileName);// 原始文件名称
                    string fileExtension = Path.GetExtension(fileName); // 文件扩展名
                    string saveName = Guid.NewGuid().ToString() + fileExtension; // 保存文件名称
                    var path = filePath + saveName;
                    file1.SaveAs(path);
                    statuscode = GetResultCode(new Model.Common.ResultCode<string> { statusCode = ((int)StatusEnum.Succeed).ToString(), Data = saveName, message = "上传成功" });
                }
                catch (Exception ex)
                {
                    statuscode = GetFailCode("上传失败", "", "", "");
                }
            }
            else
            {
                statuscode = GetFailCode("上传失败，请选择上传的图片", "", "", "");
            }
            return Content(statuscode);
        }
    }
}
